package cn.wen.yaling.xo.service.impl;

import cn.wen.yaling.commons.enums.TodoStatus;
import cn.wen.yaling.commons.utils.ResultEntity;
import cn.wen.yaling.commons.vo.TodoAddVO;
import cn.wen.yaling.commons.vo.TodoAdminListVO;
import cn.wen.yaling.commons.vo.TodoUpdateVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.wen.yaling.commons.utils.PageUtils;
import cn.wen.yaling.commons.utils.Query;

import cn.wen.yaling.xo.dao.TodoDao;
import cn.wen.yaling.commons.entity.TodoEntity;
import cn.wen.yaling.xo.service.TodoService;


@Service("todoService")
public class TodoServiceImpl extends ServiceImpl<TodoDao, TodoEntity> implements TodoService {

    @Autowired
    private TodoDao todoDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TodoEntity> page = this.page(
                new Query<TodoEntity>().getPage(params),
                new QueryWrapper<TodoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<TodoAdminListVO> getTodoListByDone(boolean done) {
        QueryWrapper<TodoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("todo_status", TodoStatus.PUBLISHED.getStatus());
        queryWrapper.eq("is_done", done);
        List<TodoEntity> todoEntities = todoDao.selectList(queryWrapper);
        if (todoEntities.size() == 0) return new ArrayList<>();
        return todoEntities.stream().map((item) -> {
            TodoAdminListVO target = new TodoAdminListVO();
            BeanUtils.copyProperties(item, target);
            return target;
        }).collect(Collectors.toList());
    }

    @Override
    public ResultEntity addTodo(TodoAddVO todoVO) {
        TodoEntity target = new TodoEntity();
        BeanUtils.copyProperties(todoVO, target);
        target.setCreateTime(new Date());
        target.setUpdateTime(new Date());
        target.setIsDone(false);
        target.setTodoStatus(TodoStatus.PUBLISHED.getStatus());
        boolean res = todoDao.insert(target) > 0;
        if (res) return ResultEntity.ok();
        return ResultEntity.error();
    }

    @Override
    public ResultEntity updateTodo(TodoUpdateVO todoVO) {
        TodoEntity target = new TodoEntity();
        BeanUtils.copyProperties(todoVO, target);
        target.setUpdateTime(new Date());
        boolean res = todoDao.updateById(target) > 0;
        if (res) return ResultEntity.ok();
        return ResultEntity.error();
    }

    @Override
    public ResultEntity activeTodo(List<Integer> todoList) {
        boolean res = todoDao.updateBatchById(todoList);
        if (res) return ResultEntity.ok();
        return ResultEntity.error();
    }

}